<template>
  <div class="my">
    <div class="m-box">
      <div class="m-ucenter-top">
        <div class="m-ucenter-photo">
          <div class="m-ucenter-img">
            <van-image
              round
              width="13.6vw"
              height="13.6vw"
              :src="user.avatar"
            />
            <!-- <img src="../../assets/default.png" /> -->
          </div>
          <div class="m-ucenter-name">
            <ul>
              <li class="m-user">{{ user.userName }} ♔</li>
              <li>{{ user.phone }}</li>
            </ul>
          </div>
        </div>
      </div>
      <div class="m-ucenter">
        <div class="m-ucenter-order" @click="vieworder">
          <span class="text-left">我的订单</span>
          <span class="text-right"> 全部订单 <i>&nbsp;</i> </span>
        </div>
        <div class="m-nav-order">
          <ul>
            <li>
              <img src="../../assets/payment.png" alt="" />
              <span>待付款</span>
            </li>
            <li>
              <img src="../../assets/received.png" alt="" />
              <span>待收货</span>
            </li>
            <li>
              <img src="../../assets/returned.png" alt="" />
              <span>退换修</span>
            </li>
          </ul>
        </div>
        <div class="m-ucenter-mun">
          <ul>
            <li class="m-xs-m" @click="Mydiscount">
              <div class="pull-left">
                <img src="../../assets/Preferential.png" alt="" />
                <span>我的优惠</span>
              </div>
              <span class="pull-right"></span>
            </li>
            <li class="m-xs-m" @click="serve">
              <div class="pull-left">
                <img src="../../assets/serve.png" alt="" />
                <span>服务中心</span>
              </div>
              <span class="pull-right"></span>
            </li>
            <li class="m-xs-m">
              <div class="pull-left" @click="btn">
                <img src="../../assets/set.png" alt="" />
                <span>设置</span>
                <!-- <van-cell is-link title="设置" @click="show = true" /> -->
              </div>
              <span class="pull-right"></span>
            </li>
          </ul>
        </div>
      </div>
      <van-action-sheet v-model="show" cancel-text="取消" close-on-click-action>
        <div class="content" @click="modify">修改信息</div>
        <div class="content" @click="addressedit">收货地址管理</div>
        <div class="content" @click="offline">退出登录</div>
      </van-action-sheet>
    </div>
  </div>
</template>
<script>
import { getuserinfor } from "@/http/api";
import { Toast, Dialog } from "vant";
import { mapState } from "vuex";
import store from "@/store";
export default {
  name: "My",
  components: {
    [Dialog.Component.name]: Dialog.Component,
  },
  data() {
    return {
      isShow: false,
      user: "",
      show: false,
    };
  },
  computed: {},
  methods: {
    toggleNav() {
      this.isShow = !this.isShow;
    },
    jumpPage(path) {
      console.log(path);
      this.$router.push({ path });
    },
    btn() {
      if (this.show) {
        this.show = false;
      } else {
        this.show = true;
      }
    },
    onCancel() {
      Toast("取消");
    },
    //获取用户信息
    userinit() {
      let that = this;
      let token = localStorage.getItem("token");
      // console.log(token);
      getuserinfor("/user/userinfor", { token }).then((data) => {
        // console.log(data);
        if (data.code == 200) {
          let { userName, email, phone,avatar } = data;
          store.dispatch("user/setEmailAsync", email);
          store.dispatch("user/setPhoneAsync", phone);
          store.dispatch("user/setUserNameAsync", userName);
          store.dispatch("user/setAvatarAsync", avatar);
          // console.log(userName,email,phone);
          that.usershow(data);
        }
      });
    },
    //渲染用户信息
    usershow(data) {
      this.user = data;
      // console.log(this.user);
    },
    //退出登录
    offline() {
      Dialog.confirm({
        title: "确定要退出登录吗",
        // message: "弹窗内容",
      })
        .then(() => {
          // on confirm
          localStorage.removeItem("token");
          localStorage.removeItem("address");
          localStorage.removeItem("shop");
          localStorage.removeItem("allorder");
          // localStorage.clear();
          this.$router.push({ path: "/login" });
        })
        .catch(() => {
          // on cancel
        });
      this.btn();
    },
    //修改用户信息
    modify(){
      this.$router.push({ path: "/modify" });  
    },
    //收货地址管理
    addressedit(){
      this.$router.push({ path: "/address" });
    },
    //查看订单
    vieworder(){
      this.$router.push({ path: "/allorder" });
    },
    //我的优惠
    Mydiscount(){
      Toast("开发中...");
    },
    //服务中心
    serve(){
      Toast("开发中...");
    },
  },
  mounted() {
    // console.log(store.state.user);
    this.userinit();
  },
};
</script>
<style lang="scss">
@import "../../static/css/base.scss";
@import "../../static/css/my.scss";
.content {
  padding: 16px 16px;
  font-size: 16px;
}
</style>